package com.rchat.pocmini.activity.changepwd;

import android.content.Intent;

import com.airtalkee.sdk.AirtalkeeAccount;
import com.aso.common.http.bean.BaseBean;
import com.aso.common.http.delegate.HttpCallbackImpl;
import com.rchat.pocmini.R;
import com.rchat.pocmini.activity.login.LoginMvpActivity;
import com.rchat.pocmini.global.HttpCodeGlobal;
import com.rchat.pocmini.global.UrlsGlobal;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by ljd on 2017/8/31.
 */

public class ChangePwdPresenterImpl extends ChangePwdContract.Presenter {

    private boolean isRequest = false;

    @Override
    void changePwd(String oldPwd, String newPwd, String newPwdConfirm) {

        if (isRequest) {
            mView.showShortToast(R.string.requesting);
            return;
        }

        if (oldPwd.equals("") || newPwd.equals("") || newPwdConfirm.equals("")) {
            mView.showShortToast(R.string.input_empty_error);
            return;
        }
        if (!newPwd.equals(newPwdConfirm)) {
            mView.showShortToast(R.string.old_new_pwd_no_same_error);
            return;
        }
        isRequest = true;

        mModel.changePwd(AirtalkeeAccount.getInstance().getUserId(), newPwd, oldPwd, new HttpCallbackImpl<BaseBean>() {
            @Override
            public void onStart() {
                mView.showPb(true);
            }

            @Override
            public void onSuccess(BaseBean baseBean) {
                if (mView == null) {
                    return;
                }
                isRequest = false;
                mView.showShortToast(baseBean.getDesc());
                goToLoginActivity();
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                isRequest = false;
                mView.showShortToast(bean.getDesc());
            }

            @Override
            public void onFinish() {
                if (null == mView) {
                    return;
                }
                mView.showPb(false);
            }
        });

    }

    private void goToLoginActivity() {
        Intent intent = new Intent();
        intent.setClass(mActivity, LoginMvpActivity.class);
        mActivity.startActivity(intent);
    }
}
